/**
 * 利用十个线程打印1-10
 */

public class _1SynchronizePrint {
    public static void main(String[] args) {
        for (int i = 1; i < 10; i++) {
            myThread thread = new myThread(i);
            thread.start();
        }

    }

    static class myThread extends Thread{

        int num ;
        static int orderNum = 1;
        static final Object obj = new Object();


        public myThread(int num){
            this.num = num;
        }

        @Override
        public void run() {

            synchronized (obj){
                while (num != orderNum){
                    System.out.println(Thread.currentThread().getName()+"is waiting");
                    try {
                        obj.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                System.out.println(Thread.currentThread().getName()+"is out"+orderNum);
                orderNum++;

                obj.notifyAll();
            }

        }
    }
}
